package com.qf.rabbitmq.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitTemplateConfirmAndReturnCallBackConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @PostConstruct   // init-method
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }


   // @PreDestroy  destroy-method  <bean id=""  class=""  init-method=""  destroy-method=""  scope=""

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if(ack){
            // 消息发送到exchange了
            System.out.println("消息发送到exchange了");
        }else{
            // 消息没法送达到exchange
            System.out.println("消息没法送达到exchange");
        }
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息没有路由到Queue中....");
    }
}
